function GetXmlHttpObject()
{
	var xmlHttp = null;
	try
	{
	// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e)
	{
	// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

var xmlHttp;
var strAddedName;
var strSelector;
var childSelector;
var strTable;
var strSelected;
var strSelectedId;

function ajaxAddItem(selector, table, field, message)
{
	//check for correct function input
	if(!selector || !table || !field || !message) 
		return;

	//Get input value
	var answer = prompt (message, "");
	if (answer == null || answer.trim() == "")
		return;
	
	strAddedName = answer;
	strSelector = selector;
	
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
			
	var url = table + "?mode=quickInsert&" + field + "=" + encodeURIComponent(strAddedName);
	xmlHttp.onreadystatechange = stateAddItemChanged;
	
	xmlHttp.open("GET", url, true);
	xmlHttp.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");
	xmlHttp.send(null);
}

function stateAddItemChanged()
{
	if (xmlHttp.readyState == 4)
	{
		var xmlDoc = xmlHttp.responseXML.documentElement; 
		var abc = xmlDoc.getElementsByTagName("id");
		var strId = xmlDoc.getElementsByTagName("id")[0].childNodes[0].nodeValue;
		
		$(strSelector).append( 
				$('<option selected/>').val(strId).html(strAddedName) 
			); 
		$.hyjack_select.update(strSelector);
		
	}
}

function ajaxAddItemTwoSelectors(selector1, selector2,table, field, message)
{
	//check for correct function input
	if(!selector1 || !table || !field || !message) 
		return;

	//Get input value
	var answer = prompt (message, "");
	if (answer == null || answer.trim() == "")
		return;
	
	strAddedName = answer;
	strSelector1 = selector1;
	strSelector2 = selector2;
	
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
			
	var url = table + "?mode=quickInsert&" + field + "=" + encodeURIComponent(strAddedName);
	xmlHttp.onreadystatechange = stateAddItemChangedTwoSelectors;
	
	xmlHttp.open("GET", url, true);
	xmlHttp.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");
	xmlHttp.send(null);
}

function stateAddItemChangedTwoSelectors()
{
	if (xmlHttp.readyState == 4)
	{
		var xmlDoc = xmlHttp.responseXML.documentElement; 
		var abc = xmlDoc.getElementsByTagName("id");
		var strId = xmlDoc.getElementsByTagName("id")[0].childNodes[0].nodeValue;
		
		$(strSelector1).append( 
				$('<option selected/>').val(strId).html(strAddedName) 
			); 
		$.hyjack_select.update(strSelector1);
		
		
		$(strSelector2).append( 
				$('<option selected/>').val(strId).html(strAddedName) 
			); 
		$.hyjack_select.update(strSelector2);
	}
}

function getCascadeDropdowList(parentSelector, Selector, table,selected,selectedId) {
	alert("Loading "+table+" list");
	childSelector=Selector;
	strTable=table;
	if( ($(parentSelector).val().trim()=="") || (parseInt($(parentSelector).val().trim())<1))
	{
		$(".selectOption"+strTable).remove();
		$.hyjack_select.update(strSelector);
//		if(parseInt($(parentSelector).val().trim())<1)
//				alert("null");
		return;
	}
	if(selected!=null)
	{
		strSelected=selected;
		strSelectedId=selectedId;
	}
	
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	
	var url = strTable + "?mode=getCascadeDropdowList&idParent=" + encodeURIComponent($(parentSelector).val());
	xmlHttp.onreadystatechange = stateParentChanged;
	
	xmlHttp.open("GET", url, true);
	xmlHttp.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");
	xmlHttp.send(null);
}

function stateParentChanged()
{
	if (xmlHttp.readyState == 4)
	{
		
		var xmlDoc = xmlHttp.responseXML.documentElement; 
		
		var strSize = xmlDoc.getElementsByTagName("size")[0].childNodes[0].nodeValue;
		var size=parseInt(strSize);
		//var strOptionClass='.selectOption'+strTable;
	
		$('.selectOption'+strTable).remove();
		for(var i=0;i<size;i++)
		{
			var strName = xmlDoc.getElementsByTagName("name"+i)[0].childNodes[0].nodeValue;
			var strId = xmlDoc.getElementsByTagName("id"+i)[0].childNodes[0].nodeValue;
			if(strId == strSelectedId)
			{
				$(childSelector).append( 
						$('<option class=selectOption'+strTable+' selected />').val(strSelectedId).html(strSelected) 
			); 
			}
			else
			{
				$(childSelector).append( 
						$('<option class=selectOption'+strTable+' />').val(strId).html(strName) 
			); 
			}
		}
		$.hyjack_select.update(strSelector);
//		alert(childSelector.val());
		
	}
}

function ajaxSendTagItem(table,id, field, mode, tag,oldTagList)
{

	
	
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
			
	var url = "tag?table="+table+"&id="+id+"&field="+field+"&mode="+mode+"&tag="+tag+"&oldTagList="+oldTagList;
	xmlHttp.onreadystatechange = stateSendTagChanged;
	
	xmlHttp.open("GET", url, true);
	xmlHttp.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");
	xmlHttp.send(null);
}

function stateSendTagChanged()
{
	if (xmlHttp.readyState == 4)
	{
		
		var xmlDoc = xmlHttp.responseXML.documentElement; 
	
		var error = xmlDoc.getElementsByTagName("error")[0].childNodes[0].nodeValue;
	
		var formattedTag = xmlDoc.getElementsByTagName("formattedTag")[0].childNodes[0].nodeValue;
	
		var oldTagList = xmlDoc.getElementsByTagName("oldTagList")[0].childNodes[0].nodeValue;
	
		var newTagList = xmlDoc.getElementsByTagName("newTagList")[0].childNodes[0].nodeValue;
		if(newTagList=='null')
			newTagList='';
		$('#tags').importTags(newTagList);
		alert('tag: '+formattedTag);
		if(error!='0')
			alert('not allow \'\,\' character.  Error='+error);
		updateOldTagList();
	}
}

function ajaxSendNoteItem(mode,id,text,x, y, width,height)
{

	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
			
	var url = "note?mode="+mode+"&id="+id+"&text="+text+"&x="+x+"&y="+y+"&width="+width+"&height="+height;
//	xmlHttp.onreadystatechange = stateSendTagChanged;
	
	xmlHttp.open("GET", url, true);
	xmlHttp.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");
	xmlHttp.send(null);
}
